package at.socialconference.web.client.ui;

import at.socialconference.inter.model.Conference;
import at.socialconference.inter.model.Place;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ShowConferencePopup extends PopupPanel {
	public ShowConferencePopup(Conference c) {
		this.setGlassEnabled(true);
		VerticalPanel vp = new VerticalPanel();
		FlexTable details = new FlexTable();
		this.add(vp);
		vp.add(details);
		int i = -1;
		if (c != null) {
			final Place place = c.getPlace();
			details.setWidget(++i, 0, new Label("Conference:"));
			details.setWidget(i, 1, new Label(c.getName()));

			details.setWidget(++i, 0, new Label("Description:"));
			details.setWidget(i, 1, new Label(c.getDescription()));

			details.setWidget(++i, 0, new Label("Date:"));
			String from = DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT).format(c.getDate_from());
			String to = DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT).format(c.getDate_to());
			details.setWidget(i, 1, new Label(from + " - " + to));

			details.setWidget(++i, 0, new Label("Place:"));
			if (c.getPlace() != null && c.getPlace().getLatitude() != null && c.getPlace().getLatitude() != 0.0
					&& c.getPlace().getLongitude() != null && c.getPlace().getLongitude() != 0.0) {
				Button btn = new Button();
				btn.setText(c.getPlace().getName());
				btn.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {
						new MapPopUp(place);
					}
				});
				details.setWidget(i, 1, btn);
			} else {
				details.setWidget(i, 1, new Label(c.getPlace() != null ? c.getPlace().getName() : ""));
			}
			String code = "002bb00" + c.getId();

			details.setWidget(++i, 0, new Label("QRCode:"));
			Image img = new Image("QRCodeServlet?message=" + code);
			img.setPixelSize(125, 125);
			details.setWidget(i, 1, img);
			this.center();
			HorizontalPanel hp = new HorizontalPanel();
			Button btnBack = new Button("Back");
			btnBack.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					ShowConferencePopup.this.hide();
				}
			});
			hp.add(btnBack);
			hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			vp.add(hp);
		}
	}
}
